;; buffer切换
(global-set-key (kbd "<backtab>") 'previous-buffer)
(global-set-key (kbd "C-<tab>") 'next-buffer)

;; 标记起始
(global-set-key (kbd "C-.") 'set-mark-command)
(global-set-key (kbd "C-,") 'set-mark-command)

;; 往回翻页，M-v和终端快捷键冲突
(global-set-key (kbd "M-n") 'scroll-down-command)

;; 设置M-g为goto-line
(global-set-key (kbd "M-g") 'goto-line)

;; 中键粘贴
(global-set-key (kbd "<mouse-2>") 'mouse-yank-at-click)

;; ctrl+鼠标左键拖动缓冲区
(require 'mouse-drag)
(global-set-key (kbd "<C-down-mouse-1>") 'mouse-drag-drag)

;; shift+鼠标左键调出设置字体的菜单
(global-set-key (kbd "<S-down-mouse-1>") 'mouse-set-font)

;; Ctrl+鼠标右键调出主菜单
(global-set-key (kbd "<C-mouse-3>") 'mouse-popup-menubar)

;; shift+鼠标右键调出选择切换buffer功能
(global-set-key (kbd "<S-mouse-3>") 'mouse-buffer-menu)

;; 鼠标滑轮每次只滑5行
(defun up-slightly ()
  (interactive)
  (scroll-up 5)
)
(defun down-slightly ()
  (interactive)
  (scroll-down 5)
)
(global-set-key [mouse-4] 'down-slightly)
(global-set-key [mouse-5] 'up-slightly)

;; smex
(require 'smex)
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;; old M-x.
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)

;; buffer切换
(global-set-key (kbd "C-p")  'bs-cycle-previous)
(global-set-key (kbd "C-n")  'bs-cycle-next)

;; ******** tab ********
(defun starsoul-tab (&optional arg)
  (interactive "P")
  (let ((count (prefix-numeric-value arg)))
    (if (use-region-p)
      (delete-forward-char count)
    )
    (if indent-tabs-mode
      (insert-char ?\t count)
      (indent-to (* tab-width (+ count (/ (current-column) tab-width))))
    )
  )
)
(global-set-key (kbd "<tab>") 'starsoul-tab)
(global-set-key [?\C-i] 'starsoul-tab)

;; ******** indent-region ********
(defun starsoul-indent-region (N)
  (interactive "p")
  (if (use-region-p)
    (progn
      (indent-rigidly (region-beginning) (region-end) (* N tab-width))
      (setq deactivate-mark nil)
    )
    (progn
      (self-insert-command N)
    )
  )
)

(defun starsoul-unindent-region (N)
  (interactive "p")
  (if (use-region-p)
    (progn
      (indent-rigidly (region-beginning) (region-end) (* N (- tab-width)))
      (setq deactivate-mark nil)
    )
    (progn
      (self-insert-command N)
    )
  )
)
(global-set-key [?\C->] 'starsoul-indent-region)
(global-set-key [?\C-<] 'starsoul-unindent-region)

;; ******** cut, copy, paste ********
;; 如果选择了一个区域
(defun starsoul-yank (N)
  (interactive "p")
  (if (use-region-p)
    (progn
      (delete-forward-char N)
      (yank)
    )
    (progn
      (yank)
    )
  )
)
(global-set-key (kbd "C-y") 'starsoul-yank)

;; ******** insert ********
(defun starsoul-insert-command (N)
  (interactive "p")
  (if (use-region-p)
    (progn
      (delete-forward-char N)
      (self-insert-command N))
    (progn
      (self-insert-command N))))

;; (global-set-key [?a] 'starsoul-insert-command)
;; (global-set-key [?b] 'starsoul-insert-command)
;; (global-set-key [?c] 'starsoul-insert-command)
;; (global-set-key [?d] 'starsoul-insert-command)
;; (global-set-key [?e] 'starsoul-insert-command)
;; (global-set-key [?f] 'starsoul-insert-command)
;; (global-set-key [?g] 'starsoul-insert-command)
;; (global-set-key [?h] 'starsoul-insert-command)
;; (global-set-key [?i] 'starsoul-insert-command)
;; (global-set-key [?j] 'starsoul-insert-command)
;; (global-set-key [?k] 'starsoul-insert-command)
;; (global-set-key [?l] 'starsoul-insert-command)
;; (global-set-key [?m] 'starsoul-insert-command)
;; (global-set-key [?n] 'starsoul-insert-command)
;; (global-set-key [?o] 'starsoul-insert-command)
;; (global-set-key [?p] 'starsoul-insert-command)
;; (global-set-key [?q] 'starsoul-insert-command)
;; (global-set-key [?r] 'starsoul-insert-command)
;; (global-set-key [?s] 'starsoul-insert-command)
;; (global-set-key [?t] 'starsoul-insert-command)
;; (global-set-key [?u] 'starsoul-insert-command)
;; (global-set-key [?v] 'starsoul-insert-command)
;; (global-set-key [?w] 'starsoul-insert-command)
;; (global-set-key [?x] 'starsoul-insert-command)
;; (global-set-key [?y] 'starsoul-insert-command)
;; (global-set-key [?z] 'starsoul-insert-command)
;; (global-set-key [?A] 'starsoul-insert-command)
;; (global-set-key [?B] 'starsoul-insert-command)
;; (global-set-key [?C] 'starsoul-insert-command)
;; (global-set-key [?D] 'starsoul-insert-command)
;; (global-set-key [?E] 'starsoul-insert-command)
;; (global-set-key [?F] 'starsoul-insert-command)
;; (global-set-key [?G] 'starsoul-insert-command)
;; (global-set-key [?H] 'starsoul-insert-command)
;; (global-set-key [?I] 'starsoul-insert-command)
;; (global-set-key [?J] 'starsoul-insert-command)
;; (global-set-key [?K] 'starsoul-insert-command)
;; (global-set-key [?L] 'starsoul-insert-command)
;; (global-set-key [?M] 'starsoul-insert-command)
;; (global-set-key [?N] 'starsoul-insert-command)
;; (global-set-key [?O] 'starsoul-insert-command)
;; (global-set-key [?P] 'starsoul-insert-command)
;; (global-set-key [?Q] 'starsoul-insert-command)
;; (global-set-key [?R] 'starsoul-insert-command)
;; (global-set-key [?S] 'starsoul-insert-command)
;; (global-set-key [?T] 'starsoul-insert-command)
;; (global-set-key [?U] 'starsoul-insert-command)
;; (global-set-key [?V] 'starsoul-insert-command)
;; (global-set-key [?W] 'starsoul-insert-command)
;; (global-set-key [?X] 'starsoul-insert-command)
;; (global-set-key [?Y] 'starsoul-insert-command)
;; (global-set-key [?Z] 'starsoul-insert-command)
;; (global-set-key [?1] 'starsoul-insert-command)
;; (global-set-key [?2] 'starsoul-insert-command)
;; (global-set-key [?3] 'starsoul-insert-command)
;; (global-set-key [?4] 'starsoul-insert-command)
;; (global-set-key [?5] 'starsoul-insert-command)
;; (global-set-key [?6] 'starsoul-insert-command)
;; (global-set-key [?7] 'starsoul-insert-command)
;; (global-set-key [?8] 'starsoul-insert-command)
;; (global-set-key [?9] 'starsoul-insert-command)
;; (global-set-key [?0] 'starsoul-insert-command)
;; (global-set-key [?`] 'starsoul-insert-command)
;; (global-set-key [?~] 'starsoul-insert-command)
;; (global-set-key [?!] 'starsoul-insert-command)
;; (global-set-key [?@] 'starsoul-insert-command)
;; (global-set-key [?#] 'starsoul-insert-command)
;; (global-set-key [?$] 'starsoul-insert-command)
;; (global-set-key [?%] 'starsoul-insert-command)
;; (global-set-key [?^] 'starsoul-insert-command)
;; (global-set-key [?&] 'starsoul-insert-command)
;; (global-set-key [?*] 'starsoul-insert-command)
;; (global-set-key [?(] 'starsoul-insert-command)
;; (global-set-key [?)] 'starsoul-insert-command)
;; (global-set-key [?-] 'starsoul-insert-command)
;; (global-set-key [?_] 'starsoul-insert-command)
;; (global-set-key [?+] 'starsoul-insert-command)
;; (global-set-key [?=] 'starsoul-insert-command)
;; (global-set-key [?\[] 'starsoul-insert-command)
;; (global-set-key [?{] 'starsoul-insert-command)
;; (global-set-key [?]] 'starsoul-insert-command)
;; (global-set-key [?}] 'starsoul-insert-command)
;; (global-set-key [?\\] 'starsoul-insert-command)
;; (global-set-key [?|] 'starsoul-insert-command)
;; (global-set-key [?\;] 'starsoul-insert-command)
;; (global-set-key [?:] 'starsoul-insert-command)
;; (global-set-key [?\'] 'starsoul-insert-command)
;; (global-set-key [?\"] 'starsoul-insert-command)
;; (global-set-key [?,] 'starsoul-insert-command)
;; (global-set-key [?<] 'starsoul-insert-command)
;; (global-set-key [?.] 'starsoul-insert-command)
;; (global-set-key [?>] 'starsoul-insert-command)
;; (global-set-key [?/] 'starsoul-insert-command)
;; (global-set-key [??] 'starsoul-insert-command)
;; (global-set-key [<SPC>] 'starsoul-insert-command)

;; ******** search & replace ********
(define-key isearch-mode-map [?\M-s] 'isearch-forward-regexp)
(define-key isearch-mode-map [?\M-r] 'isearch-backward-regexp)
(global-set-key [?\M-s ?\M-q] 'query-replace)
(global-set-key [?\M-s ?\M-r] 'query-replace-regexp)
(global-set-key [?\M-s ?\M-c] 'toggle-case-fold-search)

;; ******** upcase & downcase ********
(put 'downcase-region 'disabled nil) ;; C-x C-l
(put 'upcase-region 'disabled nil) ;; C-x C-u

;; ******** comment & uncomment ********
(global-set-key [?\M-s ?\M-m] 'comment-or-uncomment-region)
(global-set-key [?\C-c ?\C-c] 'comment-or-uncomment-region)

;; ******** 取消eww-search-words的绑定 ********
(global-set-key [?\M-s ?\M-w] nil)

;; ******** 删除选中区域 ********
(delete-selection-mode t)

;; ********  ********
(global-unset-key (kbd "C-SPC"))

(provide 'init_global-keymap)
